package com.yaj.myfactorysupport.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaj.myfactorysupport.entity.TSmtRoll;
import com.yaj.myfactorysupport.mapper.TSmtRollMapper;
import com.yaj.myfactorysupport.service.TSmtRollService;
import com.yaj.myfactorysupport.util.ErrorCodeEnum;
import com.yaj.myfactorysupport.util.RespBean;
import com.yaj.myfactorysupport.util.SucessCodeEnum;
import com.yaj.myfactorysupport.vo.TsmtRollVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wei
 * @since 2021-06-05
 */
@Slf4j
@Service
public class TSmtRollServiceImpl extends ServiceImpl<TSmtRollMapper, TSmtRoll> implements TSmtRollService {
    @Resource
    private TSmtRollMapper tSmtRollMapper;

    @Override
    public RespBean RegSMTRoll(TSmtRoll tSmtRoll) {
        try {
            int result = tSmtRollMapper.insert(tSmtRoll);

            if(result ==1){
                return RespBean.success(SucessCodeEnum.插入成功.getMsg());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return RespBean.error(ErrorCodeEnum.插入记录失败.getCode(),ErrorCodeEnum.插入记录失败.getMsg());
    }

    @Override
    public RespBean GetRegisterParts(String OrderNo, BigDecimal BrNo, String MounterID, String SMTSide,String SmtProcess) {
        QueryWrapper<TSmtRoll> qw = new QueryWrapper<>();
        qw.select("ORDER_NO,BR_NO,MOUNTER_ID,SMT_SIDE,PART_ID,LOT_NO,ROLL_NO");
        qw.lambda().eq(TSmtRoll::getOrderNo, OrderNo)
                .eq(TSmtRoll::getBrNo,BrNo)
                .eq(TSmtRoll::getMounterId,MounterID)
                .eq(TSmtRoll::getSmtSide,SMTSide)
                .eq(TSmtRoll::getSmtProcess,SmtProcess);
        List<TSmtRoll> tSmtRolls = tSmtRollMapper.selectList(qw);

        List<TsmtRollVo> tsmtRollVos = new ArrayList<TsmtRollVo>();

        if(ObjectUtil.isNotNull(tSmtRolls)){
            for (TSmtRoll tSmtRoll:
            tSmtRolls) {
                TsmtRollVo tSmtRollVo = new TsmtRollVo();
                BeanUtil.copyProperties(tSmtRoll,tSmtRollVo);
                tsmtRollVos.add(tSmtRollVo);
            }
        }

        return RespBean.success(tsmtRollVos);
    }

    @Override
    public RespBean GetReplenishGoods(String OrderNo, BigDecimal BrNo, String MounterID, String SMTSide) {
        List<TsmtRollVo> tsmtRollVos = tSmtRollMapper.getReplenishGoods(OrderNo, BrNo, MounterID, SMTSide);

        if(ObjectUtil.isNull(tsmtRollVos)){
            return RespBean.error(ErrorCodeEnum.没有找到对应PCBID的部品记录.getCode(),ErrorCodeEnum.没有找到对应PCBID的部品记录.getMsg());
        }

        return RespBean.success(tsmtRollVos);
    }
}
